package com.etlmaster.datax.plugins.tranformer;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.dp.nebula.wormhole.common.interfaces.ILine;
import com.dp.nebula.wormhole.common.interfaces.IParam;
import com.etlmaster.executor.utils.JsonHelper;
import com.etlmaster.executor.utils.LogWriter;

/**
 * 行级处理器入口
 * @author wugf
 * @date 2016-04-29
 */
public class LineTransFormer {
	public static List processorList=new ArrayList();
	public static void paraseJob(String changeMap,IParam param){
		List codeList=(List) JsonHelper.getInstance().read(changeMap);
		List transformerList=new ArrayList();
		for(int i=0;i<codeList.size();i++){
			Map<String,String> codeMap=(Map<String, String>) codeList.get(i);
			int index=Integer.parseInt(codeMap.get("index"));
			LineTransProcessor processor = null;
			String changeCode=codeMap.get("changeCode");
			String className = datax_INST_CLASS_MAPP.get(changeCode);
			if (className == null) {
				System.out.println("步骤指令{}错误，程序终止"+changeCode);
			} else {
				try{
				Class cls=Class.forName(className);
				String paramStr=param.getValue(changeCode+index);
				Constructor<LineTransProcessor> constructor = cls.getConstructor(
						Integer.class, String.class,String.class,IParam.class);
				processor=constructor.newInstance(index,changeCode,paramStr,param);
				}catch(Exception e){
					LogWriter.addLog("ERROR","生成转换实例时失败,{}",e.getMessage());
				}
			
			}
			processorList.add(processor);
			
		}
	}
	public static void trans(ILine line,IParam param){
		for(int i=0;i<processorList.size();i++){
			LineTransProcessor processor=(LineTransProcessor) processorList.get(i);
			if(!processor.isSkip())
				processor.transField(line,param);
		}
	}

	public static Map<String, String> datax_INST_CLASS_MAPP = new HashMap<String, String>();
	static {
		autoScanComponentInstruction();
	}

	public static void clearAll(){
		processorList.clear();
	}
	public static void autoScanComponentInstruction() {
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		String basePkg = "com/asiainfo/dacp/datax/plugins/tranformer";
		String packageSearchPath = "classpath*:" + basePkg + "**/*.class";
		try {
			Class.forName("org.springframework.core.io.Resource");
			Resource[] resources = resourcePatternResolver
					.getResources(packageSearchPath);
			for (Resource resource : resources) {
				String uri = resource.getURI().toString();
				String className = uri.substring(uri.indexOf(basePkg),
						uri.length() - 6).replace('/', '.');
				Class cls = Class.forName(className);
				if (cls.isAnnotationPresent(ComponentAnnotation.class)) {
					String key = ((ComponentAnnotation) cls
							.getAnnotation(ComponentAnnotation.class)).value()[0];

					if (datax_INST_CLASS_MAPP.containsKey(key)) {
						System.out.println("出现重复的指令类{}与{}"
								+ datax_INST_CLASS_MAPP.get(key) + ","
								+ className);
					} else {
						datax_INST_CLASS_MAPP.put(key, className);
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
